﻿//-----------------------------------------------------------------------
// <copyright file="ScopeDependencyTypeFinder.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: ScopeDependencyTypeFinder.cs
// * history : created by qinchaoyue 2017-10-17 06:45:26
// </copyright>
//-----------------------------------------------------------------------

using Slwy.FrameCore.Reflection;
using System;
using System.Linq;

namespace Slwy.FrameCore.Dependency
{

    /// <summary>
    /// 设置实时获取模式查找类
    /// </summary>
    public class TransientDependencyTypeFinder : ITypeFinder
    {
        /// <summary>
        /// 构造一个查找器
        /// </summary>
        public TransientDependencyTypeFinder()
        {
            this.AssemblyFinder = new DirectoryAssemblyFinder();
        }

        /// <summary>
        /// 集合查找器
        /// </summary>
        public IAllAssemblyFinder AssemblyFinder { get; set; }

        /// <summary>
        /// 根据条件搜索
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public Type[] Find(Func<Type, bool> predicate)
        {
            return this.FindAll().Where(predicate).ToArray();
        }

        /// <summary>
        /// 搜索所有继承了 <see cref="ITransientDependency"/> 接口的类
        /// </summary>
        /// <returns></returns>
        public Type[] FindAll()
        {
            var assemblies = this.AssemblyFinder.FindAll();
            return assemblies.SelectMany(assembly =>
                    assembly.GetTypes().Where(type => typeof(ITransientDependency).IsAssignableFrom(type) && !type.IsAbstract))
                .Distinct().ToArray();
        }
    }
}